package com.ship.dispatch.service.impl;

import cn.hutool.core.date.DateUtil;
import com.ship.common.business.service.impl.BaseServiceImpl;
import com.ship.dispatch.bean.SpDispatchOptimizeSchemeDetail;
import com.ship.dispatch.bean.SpShipSchedule;
import com.ship.dispatch.bean.SpYearPlanSchemeDetail;
import com.ship.dispatch.bo.VirtualShipOptimizeSchemeDetailBO;
import com.ship.dispatch.mapper.SpDispatchOptimizeSchemeDetailMapper;
import com.ship.dispatch.mapper.SpShipScheduleMapper;
import com.ship.dispatch.service.SpDispatchOptimizeSchemeDetailService;
import com.ship.dispatch.util.IDUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class SpDispatchOptimizeSchemeDetailServiceImpl extends BaseServiceImpl<SpDispatchOptimizeSchemeDetailMapper, SpDispatchOptimizeSchemeDetail> implements SpDispatchOptimizeSchemeDetailService {

    @Autowired
    private SpShipScheduleMapper shipScheduleMapper;

    /**
     * 查询调度优化详情最新一条
     * @param createBy
     * @param planId
     * @return
     */
    @Override
    public SpDispatchOptimizeSchemeDetail selectMaxOptimizeSchemeDetail(String createBy, Long planId) {
        return this.getBaseMapper().selectMaxOptimizeSchemeDetail(createBy,planId);
    }

    /**
     * 虚拟船保存甘特图(调度优化)
     * @param virtualShipSchemeDetailBO
     * @return
     */
    @Override
    public SpDispatchOptimizeSchemeDetail saveOptimizeVirtualShipSchemeDetail(VirtualShipOptimizeSchemeDetailBO virtualShipSchemeDetailBO) {
        //数字要以最终下发的计划中预计装载时间顺序排序自增
        SpDispatchOptimizeSchemeDetail schemeDetail = new SpDispatchOptimizeSchemeDetail();
        schemeDetail.setCreateBy(virtualShipSchemeDetailBO.getCreateBy());
        schemeDetail.setId(IDUtil.nextId());
        schemeDetail.setSchemeId(virtualShipSchemeDetailBO.getSchemeId());
        if(virtualShipSchemeDetailBO.getConId()!=null){
            schemeDetail.setResourceContract(virtualShipSchemeDetailBO.getConId());
        }
        if(virtualShipSchemeDetailBO.getConId()!=null){
            schemeDetail.setContractName(virtualShipSchemeDetailBO.getContractName());
        }
        //schemeDetail.setResourceContract(virtualShipSchemeDetailBO.getConId());
        //schemeDetail.setContractName(virtualShipSchemeDetailBO.getContractName());
        schemeDetail.setSchemeNumber(virtualShipSchemeDetailBO.getNum());
        if(virtualShipSchemeDetailBO.getConId()==null){
            schemeDetail.setSeller("虚拟资源方");
        }else{
            schemeDetail.setSeller(virtualShipSchemeDetailBO.getSeller());
        }
        if(virtualShipSchemeDetailBO.getConId()!=null){
            schemeDetail.setBuyer(virtualShipSchemeDetailBO.getBuyer());
            schemeDetail.setExpectedLoadingPort(virtualShipSchemeDetailBO.getLoadingPort());
            schemeDetail.setExpectedUnloadingPort(virtualShipSchemeDetailBO.getUnloadingPort());
            schemeDetail.setTradeType(virtualShipSchemeDetailBO.getTradeType());
            schemeDetail.setTradeBody(virtualShipSchemeDetailBO.getTradeBody());
        }
        //schemeDetail.setBuyer(virtualShipSchemeDetailBO.getBuyer());
        //schemeDetail.setExpectedLoadingPort(virtualShipSchemeDetailBO.getLoadingPort());
        //schemeDetail.setExpectedUnloadingPort(virtualShipSchemeDetailBO.getUnloadingPort());
        //schemeDetail.setTradeType(virtualShipSchemeDetailBO.getTradeType());
        //schemeDetail.setTradeBody(virtualShipSchemeDetailBO.getTradeBody());
        schemeDetail.setGoodsNum(schemeDetail.getSeller() + "-FOB-" + virtualShipSchemeDetailBO.getNum());
//        schemeDetail.setShipRoute(shipRoute);

        //date:排列出来的装载时间
        //date = DateUtil.format(DateUtil.parse( "2024-04-13 00:00:00", "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss");

        Date parse = DateUtil.parse(virtualShipSchemeDetailBO.getDate(), "yyyy-MM-dd HH:mm:ss");
        Integer year = DateUtil.year(new Date()) + 1;
        schemeDetail.setVoyageNo("CNOOC-" + year + "-" + virtualShipSchemeDetailBO.getNum());

        schemeDetail.setExpectedLoadingDate(parse);
        schemeDetail.setExpectedLoadingCompletionWindow(DateUtil.offsetDay(parse, 2));
        schemeDetail.setShipName(virtualShipSchemeDetailBO.getShipName());
        schemeDetail.setImo(virtualShipSchemeDetailBO.getImo());
        Date loadingDate = schemeDetail.getExpectedLoadingDate();
        //中国预计卸载时间(走巴拿马和不走巴拿马)
        schemeDetail.setExpectedUnloadingWindow(DateUtil.offsetDay(loadingDate, virtualShipSchemeDetailBO.getVoyageDays()));
        //印度预计卸载时间
        schemeDetail.setIndiaUnloadTime(DateUtil.offsetDay(loadingDate, virtualShipSchemeDetailBO.getIndiaDays()));
        //欧洲预计卸载时间
        schemeDetail.setEuropeUnloadTime(DateUtil.offsetDay(loadingDate, virtualShipSchemeDetailBO.getEuropeDays()));
        //预计满载航程天数
        schemeDetail.setExpectedFullLoadVoyageDays(virtualShipSchemeDetailBO.getVoyageDays());
        //印度空载航程天数
        schemeDetail.setIndiaEmptyLoadDays(virtualShipSchemeDetailBO.getIndiaDays());
        //欧洲空载航程天数
        schemeDetail.setEuropeEmptyLoadDays(virtualShipSchemeDetailBO.getEuropeDays());
        //预计卸载窗口
        Date unload = schemeDetail.getExpectedUnloadingWindow();
        //预计卸载完成窗口
        schemeDetail.setExpectedUnloadingCompletionWindow(DateUtil.offsetDay(unload, 2));
        //预计空载航程天数
        schemeDetail.setExpectedEmptyLoadVoyageDays(virtualShipSchemeDetailBO.getVoyageDays());
        //预计卸载完成窗口
        Date unloadCom = schemeDetail.getExpectedUnloadingCompletionWindow();
        //预计空载返程窗口
        schemeDetail.setExpectedEmptyReturnWindow(DateUtil.offsetDay(schemeDetail.getExpectedUnloadingCompletionWindow(), virtualShipSchemeDetailBO.getVoyageDays()));
        schemeDetail.setCreateDate(new Date());
        schemeDetail.setSingleGoodsQuantity(virtualShipSchemeDetailBO.getSingleGoodsQuantity());
        schemeDetail.setShipSpeed(virtualShipSchemeDetailBO.getShipSpeed());
//        schemeDetail.setQuantityUnit(schemeModel.getQuantityUnit());
        schemeDetail.setPlanId(virtualShipSchemeDetailBO.getPlanId());
        schemeDetail.setOilConsumption(virtualShipSchemeDetailBO.getOilConsumption());

        int numSave = this.getBaseMapper().insert(schemeDetail);
        if (numSave > 0) {
            //保存船舶班期表
            SpShipSchedule spShipSchedule = new SpShipSchedule();
            BeanUtils.copyProperties(schemeDetail, spShipSchedule);
            spShipSchedule.setId(IDUtil.nextId());
            spShipSchedule.setStartingDateOfLeaseBegin(virtualShipSchemeDetailBO.getLeaseTime());
            Date redeliveryDateEnd = DateUtil.offsetDay(virtualShipSchemeDetailBO.getLeaseTime(),virtualShipSchemeDetailBO.getLeaseDuration());
            spShipSchedule.setRedeliveryDateEnd(redeliveryDateEnd);
            shipScheduleMapper.insert(spShipSchedule);
        }
        return schemeDetail;
    }
}
